package net.jlibrary.invoice.service;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.jlibrary.common.core.service.BaseManager;
import net.jlibrary.common.project.domain.Group;
import net.jlibrary.curbparking.domain.ParkRecord;
import net.jlibrary.invoice.domain.ElectronicInvoice;
import net.jlibrary.invoice.domain.ElectronicInvoiceGfInfo;
import net.jlibrary.invoice.domain.ElectronicInvoiceKpInfo;
import net.jlibrary.invoice.domain.Goods;
import net.jlibrary.invoice.domain.InvoiceStatus;
import net.jlibrary.invoice.domain.InvoiceType;
import net.jlibrary.invoice.domain.req.NotifyUrlReq;
import net.jlibrary.invoice.domain.resp.InvoiceDetail;
import net.jlibrary.invoice.domain.resp.NegativeInvoice;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class ElectronicInvoiceKpInfoManager extends BaseManager implements IElectronicInvoiceKpInfoManager {

	@Override
	public JSONObject getInvoice(Long groupId) {
		ElectronicInvoice electronicInvoice = new ElectronicInvoice();
		electronicInvoice.setGroup(new Group());
		electronicInvoice.getGroup().setId(groupId);
		if (dao.baseLists(electronicInvoice, false).isEmpty()) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("error", "该订单销方企业信息为空");
			return jsonObject;
		}
		electronicInvoice = (ElectronicInvoice) dao.baseLists(electronicInvoice, false).get(0);
		String invoiceString = JSON.toJSONString(electronicInvoice);// 关键
		JSONObject json = JSON.parseObject(invoiceString);
		json.remove("id");
		json.put("invoiceXfId", electronicInvoice.getId());
		return json;
	}

	@Override
	public ElectronicInvoiceKpInfo electronicInvoiceKpInfo(String orderNo) {
		ElectronicInvoiceKpInfo kpInfo = new ElectronicInvoiceKpInfo();
		kpInfo.setRecord(new ParkRecord());
		kpInfo.getRecord().setId(Long.valueOf(orderNo));
		kpInfo = (ElectronicInvoiceKpInfo) dao.baseLists(kpInfo, false).get(0);
		return kpInfo;
	}

	@Override
	public ElectronicInvoiceKpInfo invoiceKpInfo(String fpdm, String fphm) {
		ElectronicInvoiceKpInfo kpInfo = new ElectronicInvoiceKpInfo();
		kpInfo.setFpdm(fpdm);
		kpInfo.setFphm(fphm);
		kpInfo = (ElectronicInvoiceKpInfo) dao.baseLists(kpInfo, false).get(0);
		return kpInfo;
	}

	/**
	 * 查询发票详情
	 */
	@Override
	public ElectronicInvoiceKpInfo upKpInfo(ElectronicInvoiceKpInfo info, InvoiceDetail invoiceDetail) {
		ElectronicInvoiceKpInfo kpAction = new ElectronicInvoiceKpInfo();
		// 开票id
		kpAction.setId(info.getId());
		// 开票类型
		kpAction.setInvoiceType(info.getInvoiceType());
		// 销方企业id
		ElectronicInvoice invoice = new ElectronicInvoice();
		invoice.setId(Long.valueOf(info.getEiSetting().getId()));
		kpAction.setEiSetting(invoice);
		// 购方企业id
		ElectronicInvoiceGfInfo invoiceGf = new ElectronicInvoiceGfInfo();
		invoiceGf.setId(info.getGfInfo().getId());
		kpAction.setGfInfo(invoiceGf);
		// 订单id
		ParkRecord parkRecord = new ParkRecord();
		parkRecord.setId(info.getRecord().getId());
		kpAction.setRecord(parkRecord);
		kpAction.setZsid(invoiceDetail.getResultObj().getId().longValue());// 中税发票id
		kpAction.setFpdm(invoiceDetail.getResultObj().getFpdm());// 发票代码
		kpAction.setFphm(invoiceDetail.getResultObj().getFphm());// 发票号码
		kpAction.setSkm(invoiceDetail.getResultObj().getSkm());// 税控码
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date kprq = formatter.parse(invoiceDetail.getResultObj().getKprq(), pos);
		kpAction.setKprq(kprq);// 开票日期
		kpAction.setJym(invoiceDetail.getResultObj().getJym());// 校验码
		kpAction.setPdfUrl(invoiceDetail.getResultObj().getPdfUrl());// pdfUrl文件
		kpAction.setUrl(invoiceDetail.getResultObj().getUrl());// 电子发票ofd格式文件
		kpAction.setKpzdbs(invoiceDetail.getResultObj().getKpzdbs());// 开盘税控号
		kpAction.setFpqqlsh(invoiceDetail.getResultObj().getFpqqlsh());// 请求税局的发票请求流水号
		kpAction.setEwm(invoiceDetail.getResultObj().getEwm());// 二维码
		if (invoiceDetail.getResultObj().getStatus() != null) {
			InvoiceStatus invoiceStatus = new InvoiceStatus();
			invoiceStatus.setCode(invoiceDetail.getResultObj().getStatus());
			invoiceStatus = (InvoiceStatus) dao.baseLists(invoiceStatus, false).get(0);
			kpAction.setStatus(invoiceStatus);// 状态
		}
		if (!StringUtils.isBlank(invoiceDetail.getResultObj().getBz())) {
			kpAction.setBz(invoiceDetail.getResultObj().getBz());// 备注
		}
		kpAction.setErrorMsg(invoiceDetail.getResultObj().getErrorMsg());// 失败原因
		dao.baseUpdateObject(kpAction);// 将 发票接口返回的信息 填入 即修改信息
		return kpAction;
	}

	/**
	 * 暂时先固定goods商品信息,无法确定选择的订单是何种商品
	 */
	@Override
	public JSONObject getGoods() {
		Goods goods = new Goods();
		goods.setSpbm("5030000000000000000");
		goods = (Goods) dao.baseLists(goods, false).get(0);
		String goodsString = JSON.toJSONString(goods);// 关键
		JSONObject json = JSON.parseObject(goodsString);
		json.put("goodsId", goods.getId());
		json.remove("id");
		json.put("goodsId", goods.getId());
		return json;
	}

	/**
	 * 根据code 查询字典中InvoiceType发票类型的id
	 */
	@Override
	public Long invoiceType(String code) {
		InvoiceType invoiceType = new InvoiceType();
		invoiceType.setCode(code);
		invoiceType = (InvoiceType) dao.baseLists(invoiceType, false).get(0);
		return invoiceType.getId();
	}

	/**
	 * 根据code 查询字典中InvoiceStatus发票状态的id
	 */
	@Override
	public Long invoiceStatus(String code) {
		InvoiceStatus invoiceStatus = new InvoiceStatus();
		invoiceStatus.setCode(code);
		invoiceStatus = (InvoiceStatus) dao.baseLists(invoiceStatus, false).get(0);
		return invoiceStatus.getId();
	}

	/**
	 * 回调接口返回的开票信息,返回即开票成功
	 */
	@Override
	public ElectronicInvoiceKpInfo invoiceKpInfo(ElectronicInvoiceKpInfo info, NotifyUrlReq urlReq) {
		ElectronicInvoiceKpInfo kpAction = new ElectronicInvoiceKpInfo();
		// 开票id
		kpAction.setId(info.getId());
		// 开票类型
		kpAction.setInvoiceType(info.getInvoiceType());
		// 销方企业id
		ElectronicInvoice invoice = new ElectronicInvoice();
		invoice.setId(Long.valueOf(info.getEiSetting().getId()));
		kpAction.setEiSetting(invoice);
		// 购方企业id
		ElectronicInvoiceGfInfo invoiceGf = new ElectronicInvoiceGfInfo();
		invoiceGf.setId(info.getGfInfo().getId());
		kpAction.setGfInfo(invoiceGf);
		// 订单id
		ParkRecord parkRecord = new ParkRecord();
		parkRecord.setId(info.getRecord().getId());
		kpAction.setRecord(parkRecord);
		kpAction.setZsid(Long.valueOf(urlReq.getId()));// 中税发票id
		kpAction.setFpdm(urlReq.getFpdm());// 发票代码
		kpAction.setFphm(urlReq.getFphm());// 发票号码
		kpAction.setSkm(urlReq.getSkm());// 税控码
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// 注意月份是MM
		try {
			Date kprq = simpleDateFormat.parse(urlReq.getKprq());
			kpAction.setKprq(kprq);// 开票日期
		} catch (ParseException e) {
			e.printStackTrace();
		}
		kpAction.setJym(urlReq.getJym());// 校验码
		kpAction.setEwm(urlReq.getEwm());// 二维码
		kpAction.setPdfUrl(urlReq.getPdfUrl());// pdfUrl文件
		kpAction.setKpzdbs(urlReq.getKpzdbs());// 税控盘编码
		kpAction.setFpqqlsh(urlReq.getFpqqlsh());// 请求税局的发票请求流水号
		// 当有回调说明发票开具成功
		InvoiceStatus invoiceStatus = new InvoiceStatus();
		invoiceStatus.setCode("2");
		invoiceStatus = (InvoiceStatus) dao.baseLists(invoiceStatus, false).get(0);
		kpAction.setStatus(invoiceStatus);// 状态
		dao.baseUpdateObject(kpAction);// 将 发票接口返回的信息 填入 即修改信息
		return kpAction;
	}

	/**
	 * 红冲发票
	 */
	@Override
	public ElectronicInvoiceKpInfo negativeInfo(ElectronicInvoiceKpInfo info, NegativeInvoice urlReq) {
		ElectronicInvoiceKpInfo kpAction = new ElectronicInvoiceKpInfo();
		// 开票id
		kpAction.setId(info.getId());
		// 开票类型
		kpAction.setInvoiceType(info.getInvoiceType());
		// 销方企业id
		ElectronicInvoice invoice = new ElectronicInvoice();
		invoice.setId(Long.valueOf(info.getEiSetting().getId()));
		kpAction.setEiSetting(invoice);
		// 购方企业id
		ElectronicInvoiceGfInfo invoiceGf = new ElectronicInvoiceGfInfo();
		invoiceGf.setId(info.getGfInfo().getId());
		kpAction.setGfInfo(invoiceGf);
		// 订单id
		ParkRecord parkRecord = new ParkRecord();
		parkRecord.setId(info.getRecord().getId());
		kpAction.setRecord(parkRecord);
		kpAction.setZsid(urlReq.getResultObj().getId().longValue());// 中税发票id
		kpAction.setFpdm(urlReq.getResultObj().getFpdm());// 新发票代码
		kpAction.setFphm(urlReq.getResultObj().getFphm());// 新发票号码
		kpAction.setUrl(urlReq.getResultObj().getUrl());// 新ofd文件的url
		kpAction.setPdfUrl(urlReq.getResultObj().getPdfUrl());// 新pdf格式文件
		kpAction.setBz(urlReq.getResultObj().getBz());// 备注
		kpAction.setYfpdm(urlReq.getResultObj().getYfpdm());// 原发票代码
		kpAction.setYfphm(urlReq.getResultObj().getYfphm());// 原发票号码
		kpAction.setNegativeJym(urlReq.getResultObj().getNegativeJym());// 负数发票的校验码
		kpAction.setNegativeSkm(urlReq.getResultObj().getNegativeSkm());// 负数发票的税控码
		if (urlReq.getResultObj().getStatus() != null) {
			InvoiceStatus invoiceStatus = new InvoiceStatus();
			invoiceStatus.setCode(urlReq.getResultObj().getStatus());
			invoiceStatus = (InvoiceStatus) dao.baseLists(invoiceStatus, false).get(0);
			kpAction.setStatus(invoiceStatus);// 状态
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date negativeKprq = formatter.parse(urlReq.getResultObj().getNegativeKprq());// 作废日期
			kpAction.setNegativeKprq(negativeKprq);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dao.baseUpdateObject(kpAction);// 将 发票接口返回的信息 填入 即修改信息
		return kpAction;
	}

}
